<?php
/**
 *	This class provide functions used to access country table 
 */
 
class country
{
	function country() 
    {
    }
    
    /**
     * Get all country in database
     * @param ezSQL $db
     * @return array of country. 
     * Refer ntp_country table for country structure 
     */
    function getAllCountry($db)
    {
    	$sql = sprintf("select * from %s", TBL_LANG);
    	$res = $db->get_results($sql);
    	return $res;
    }
    
    /**
     * Get country information
     * @param szSQL $db	ezSQL object
     * @param string $id  country Id
     * @return country country object
     */
    function getCountryById($db, $id)
    {
    	$sql = sprintf("select * from %s where lang_key='$id'", TBL_LANG);
    	$res = $db->get_row($sql);
    	//$db->debug();
    	return $res;
    }
    
    /**
     * Get list of language of country
     * @param szSQL $db	ezSQL object
     * @param string $id  country Id
     */
    function getLanguageList($db, $id)
    {
    	$langs = null;
    	$country = country::getCountryById($db, $id);
		if($country != null)
		{
			$langs = explode(",", $country->languages);
		}
		
		return $langs;
    }
    
    /**
     * Save or update a value of option table
     * @param string $langlist the language list separate by comma
     */
    
    function updateLanguage($db, $key, $langlist)
    {
    	$sql = sprintf("update %s set " .
    					"languages = '$langlist' " .
    					"where lang_key = '$key'",
    					TBL_LANG);
    	
    	return $db->query($sql);
    }
    
    /**
     * Delete country by it's ID
     * @param string country Id
     */
    function deleteCountry($db, $id)
    {
    	$sql = sprintf("delete from %s " .
    					"where lang_key = '$id'",
    					TBL_LANG);
    	
    	return $db->query($sql);
    }
    
    function addCountry($db, $country)
    {
    	$langs = @implode("," , $country['ID']);
    	
    	$res = $db->query(sprintf(
					"INSERT INTO %s(lang_key, country_name, languages) " .
					"VALUES('%s', '%s', '%s')",
    				TBL_LANG, 
    				$country['country_code'],
    				$country['country_name'],
    				$langs));
    				
    	if($res == true)
    		return $db->insert_id;
    	else
    		return -1;
    }
}
?>